load 'training.mat'; % get trainingSet
% Set serial port
serialPortId = '/dev/tty.usbserial-FTFO9L12';% '/dev/tty.usbserial-FTFO9HPY';
serialPort = serial(serialPortId, 'BaudRate', 9600, 'DataBits', 8, 'Timeout', 1.5);
% Open Serial Port
try
    fopen(serialPort);
catch ME
    error('failed to open serial port');
end

while 1
    cmd = imput('landed ?1: yes; 0: quit');
    if cmd == 0
        break;
    end
    
    rxInfo = fscanf(serialPort)
    info = textscan(rxInfo, '%d,%d,%d,%d,%d,%d,%d,%d,%d')

        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        %   *   *   *
        %     1   2
        %   *   *   *
        %     3   4
        %   *   *   *  --> the deployment
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    % Comparing Fingerprints
    
    for i = 1 : size(trainingSet, 2)
       sumDist{i} = 0;
       for j = 1 : size(trainingSet{i}, 1)
           sumDist{i} = sumDist{i} + getDist(traningSet{i}(k), info(k));
       end
    end
    [~, target] = max([sumDist{1}, sumDist{2}, sumDist{3}, sumDist{4}]);
    % target is the area
end

% Close port
try
    stopasync(serialPort);
    fclose(serialPort);
    delete(serialPort);
    clear serialPort;
catch ME
    error('fail to close the serial port, check connection and name'); 
end